﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

namespace Nirvana
{
    [Serializable]

    public sealed class CheckRenderer 
    {
		private enum ShadowCastingCheckMode
		{
			[EnumLabel("Not Check")]
			NotCheck,
			[EnumLabel("Force Off")]
			ForceOff,
			[EnumLabel("Force On")]
			ForceOn,
			[EnumLabel("Force On But Particle")]
			ForceOnButParticle
		}
		[Tooltip("The shadow casting check mode.")]
		[EnumLabel]
		[SerializeField]
		private ShadowCastingCheckMode shadowCastingCheckMode;

		private enum ReceiveShadowsMode
		{
			[EnumLabel("Not Check")]
			NotCheck,
			[EnumLabel("Force Off")]
			ForceOff,
			[EnumLabel("Force On")]
			ForceOn,
			[EnumLabel("Force On But Particle")]
			ForceOnButParticle
		}
		[Tooltip("The receive shadow check mode.")]
		[EnumLabel]
		[SerializeField]
		private ReceiveShadowsMode receiveShadowsMode;

		private enum LightProbeUsageMode
		{
			[EnumLabel("Not Check")]
			NotCheck,
			[EnumLabel("Force Off")]
			ForceOff,
			[EnumLabel("Force BlendProbes")]
			ForceBlendProbes,
			[EnumLabel("Force UseProxyVolume")]
			ForceUseProxyVolume
		}
		[EnumLabel]
		[SerializeField]
		[Tooltip("The light probe usage check mode.")]
		private LightProbeUsageMode lightProbeUsageMode;

		private enum ReflectionProbeUsageMode
		{
			[EnumLabel("Not Check")]
			NotCheck,
			[EnumLabel("Force Off")]
			ForceOff,
			[EnumLabel("Force BlendProbes")]
			ForceBlendProbes,
			[EnumLabel("Force BlendProbesAndSkybox")]
			ForceBlendProbesAndSkybox,
			[EnumLabel("Force Simple")]
			ForceSimple
		}
		[SerializeField]
		[EnumLabel]
		[Tooltip("The reflection probe usage check mode.")]
		private ReflectionProbeUsageMode reflectionProbeUsageMode;

		private enum NirvanaRendererMode
		{
			[EnumLabel("Not Check")]
			NotCheck,
			[EnumLabel("Force Add")]
			ForceAdd,
			[EnumLabel("Force Not")]
			ForceNot,
			[EnumLabel("Force Add But Particle")]
			ForceAddButParticle
		}
		[Tooltip("The nirvana renderer mode.")]
		[EnumLabel]
		[SerializeField]
		private NirvanaRendererMode nirvanaRendererMode;

		public void CheckRender(AssetCheckReport report, Renderer renderer)
		{
			if (this.shadowCastingCheckMode == ShadowCastingCheckMode.ForceOff)
			{
				if (renderer.shadowCastingMode != ShadowCastingMode.Off)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s shadowCastingMode must turn off.", renderer.name);
				}
			}
			else if (this.shadowCastingCheckMode == ShadowCastingCheckMode.ForceOn)
			{
				if (renderer.shadowCastingMode == ShadowCastingMode.Off)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s shadowCastingMode must turn on.", renderer.name);
				}
			}
			else if (this.shadowCastingCheckMode == ShadowCastingCheckMode.ForceOnButParticle)
			{
				if (renderer.HasComponent<ParticleSystem>() || renderer is TrailRenderer)
				{
					if (renderer.shadowCastingMode != ShadowCastingMode.Off)
					{
						report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s shadowCastingMode must turn off.", renderer.name);
					}
				}
				else if (renderer.shadowCastingMode == ShadowCastingMode.Off)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s shadowCastingMode must turn on.", renderer.name);
				}
			}

			if (this.receiveShadowsMode == ReceiveShadowsMode.ForceOn)
			{
				if (!renderer.receiveShadows)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s receiveShadows must turn on.", renderer.name);
				}
			}
			else if (this.receiveShadowsMode == ReceiveShadowsMode.ForceOff)
			{
				if (renderer.receiveShadows)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s receiveShadows must turn off.", renderer.name);
				}
			}
			else if (this.receiveShadowsMode == ReceiveShadowsMode.ForceOnButParticle)
			{
				if (renderer.HasComponent<ParticleSystem>())
				{
					if (renderer.receiveShadows)
					{
						report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s receiveShadows must turn off.", renderer.name);
					}
				}
				else if (!renderer.receiveShadows)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s receiveShadows must turn on.", renderer.name);
				}
			}
			if (this.lightProbeUsageMode == LightProbeUsageMode.ForceOff)
			{
				if (renderer.lightProbeUsage != LightProbeUsage.Off)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s LightProbeUsage must turn off.", renderer.name);
				}
			}
			else if (this.lightProbeUsageMode == LightProbeUsageMode.ForceBlendProbes)
			{
				if (renderer.lightProbeUsage != LightProbeUsage.BlendProbes)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s LightProbeUsage must be BlendProbes.", renderer.name);
				}
			}
			else if (this.lightProbeUsageMode == LightProbeUsageMode.ForceUseProxyVolume && renderer.lightProbeUsage != LightProbeUsage.UseProxyVolume)
			{
				report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s LightProbeUsage must be UseProxyVolume.", renderer.name);
			}
			if (this.reflectionProbeUsageMode == ReflectionProbeUsageMode.ForceOff)
			{
				if (renderer.reflectionProbeUsage != ReflectionProbeUsage.Off)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s ReflectionProbeUsage must turn off.", renderer.name);
				}
			}
			else if (this.reflectionProbeUsageMode == ReflectionProbeUsageMode.ForceBlendProbes)
			{
				if (renderer.reflectionProbeUsage != ReflectionProbeUsage.BlendProbes)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s ReflectionProbeUsage must be BlendProbes.", renderer.name);
				}
			}
			else if (this.reflectionProbeUsageMode == ReflectionProbeUsageMode.ForceBlendProbesAndSkybox)
			{
				if (renderer.reflectionProbeUsage != ReflectionProbeUsage.BlendProbesAndSkybox)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s ReflectionProbeUsage must be BlendProbesAndSkybox.", renderer.name);
				}
			}
			else if (this.reflectionProbeUsageMode == ReflectionProbeUsageMode.ForceSimple && renderer.reflectionProbeUsage != ReflectionProbeUsage.Simple)
			{
				report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b>'s ReflectionProbeUsage must be Simple.", renderer.name);
			}

			if (this.nirvanaRendererMode == NirvanaRendererMode.ForceAdd)
			{
				NirvanaRenderer component = renderer.GetComponent<NirvanaRenderer>();
				if (component == null)
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b> must add a NirvanaRenderer.", renderer.name);
				}
				else if (component.Materials == null || component.Materials.Length == 0 || component.Materials[0] == null)
				{
					report.ShowWarnInfo("The NirvanaRenderer <b><color=orange>{0}</color></b> missing materials.", renderer.name);
				}
			}
			else if (this.nirvanaRendererMode == NirvanaRendererMode.ForceNot)
			{
				if (renderer.HasComponent<NirvanaRenderer>())
				{
					report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b> can not add a NirvanaRenderer.", renderer.name);
				}
			}
			else if (this.nirvanaRendererMode == NirvanaRendererMode.ForceAddButParticle)
			{
				if (renderer.HasComponent<ParticleSystem>() || renderer is TrailRenderer)
				{
					if (renderer.HasComponent<NirvanaRenderer>())
					{
						report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b> can not add a NirvanaRenderer.", renderer.name);
					}
				}
				else
				{
					NirvanaRenderer component2 = renderer.GetComponent<NirvanaRenderer>();
					if (component2 == null)
					{
						report.ShowWarnInfo("The Renderer <b><color=orange>{0}</color></b> must add a NirvanaRenderer.", renderer.name);
					}
					else if (component2.Materials == null || component2.Materials.Length == 0 || component2.Materials[0] == null)
					{
						report.ShowWarnInfo("The NirvanaRenderer <b><color=orange>{0}</color></b> missing materials.", renderer.name);
					}
				}


			}
		}

	}
}
